<?php

namespace Admins\Controller;

use Common\Controller\AdminBase;

class TeachController extends AdminBase
{
    public function catlist()
    {
        if(IS_POST)
        {
            $teach = M('Teachcate');
            foreach ($_POST['sort'] as $id => $sort) {
                $teach->where(array('catid' => $id))->save(array('sort' => $sort));
            }
            $this->success("排序更新成功！");
            exit;
        }
        $result = D('Admins/Teach')->getData();
        if($result)
        {
            foreach($result as $k => $v)
            {
                $result[$k]['status'] = showStatus($v['status']);
                $result[$k]['editurl'] = U("Teach/editcat", array('catid' => $v['catid']));
                $result[$k]['delurl'] = U("Teach/delcat", array('catid' => $v['catid']));
            }
        }
        $this->assign("list", $result);
        $this->display();
    }
    
    public function catadd()
    {
        if(IS_POST)
        {
            $teach = D('Admins/Teach');
            $post = I('post.');
            if ($post['isbatch']) 
            {
                $batch_add = explode("\n", $post['batch_add']);
                if (empty($batch_add) || empty($post['batch_add'])) {
                    $this->error('请填写需要添加的分类！');
                }
                unset($post['isbatch'], $post['batch_add'], $post['catname'], $post['enname']);
                //遍历数组
                foreach ($batch_add as $rs) {
                    $cat = explode('|', $rs, 2);
                    if ($cat[0] && $cat[1]) {
                        $post['catname'] = $cat[0];
                        $post['enname'] = $cat[1];
                        $teach->addCategory($post);
                    }
                }
            }
            else 
            {
                unset($post['isbatch'],$post['batch_add']);
                $cateid = $teach->addCategory($post);
                if ($cateid) {
                    $this->success("添加成功！", U("Teach/catlist"));
                } else {
                    $error = $teach->getError();
                    $this->error($error ? $error : '分类添加失败！');
                }
            }
            $this->success("添加分类成功！", U("Teach/catlist"));
        }
        else 
        {
            $this->display();
        }
    }
    
    public function editcat()
    {
        if(IS_POST)
        {
            $post = I('post.');
            $catid = $post['catid'];
            unset($post['catid']);
            $type = M('Teachcate')->where(array('catid' => $catid))->save($post);
            if($type)
            {
                $this->success("修改成功", U('Teach/catlist'));
            }
            else 
            {
                $this->error("修改失败");
            }
        }
        else 
        {
            $catid = I('get.catid', 0, 'intval');
            
            $info = D('Admins/Teach')->get_one_cat($catid);
            $this->assign("info", $info);
            $this->display();
        }
    }
    
    public function delcat()
    {
        $catid = I("get.catid", "", "intval");
        if (!$catid) {
            $this->error("请指定需要删除的栏目！");
        }
        $status = M('Teachcate')->where(array('catid' => $catid))->delete();
        if (false == $status) {
            $this->error('栏目删除失败！');
        }
        $this->success("栏目删除成功！", U("Teach/catlist"));
    }
    
    public function adlist()
    {
        $ad = M('Teachad');
        $count = $ad->count();
        
        $page = $this->page($count, 20);
        $list = $ad->limit($page->firstRow . ',' . $page->listRows)->order(array('create_time' => 'DESC'))->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                if($v['position'] == 1)
                {
                    $list[$k]['position'] = "中间";
                }
                elseif($v['position'] == 2)
                {
                    $list[$k]['position'] = "右侧上方";
                }
                elseif($v['position'] == 3)
                {
                    $list[$k]['position'] = "右侧中间";
                }
                else 
                {
                    $list[$k]['position'] = "右侧下方";
                }
            }
        }
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        
        $this->display();
    }
    
    public function adadd()
    {
        if(IS_POST)
        {
            $post = I('post.');
            if(empty($post['title']))
            {
                $this->error("请输入信息标题");
            }
            if(empty($post['image']))
            {
                $this->error("请上传广告图片");
            }
            $post['create_time'] = time();
            if(D('Admins/Teach')->adadd($post))
            {
                $this->success("添加成功", U("Teach/adlist"));
            }
            else 
            {
                $error = D('Admins/Teach')->getError();
                $this->error($error ? $error : '信息添加失败！');
            }
        }
        else 
        {
            $this->display();
        }
    }
    
    public function adedit()
    {
        if(IS_POST)
        {
            $post = I('post.');
            $ad_id = $post['ad_id'];
            unset($post['ad_id']);
            $ad = M('Teachad');
            $id = $ad->where(array('ad_id' => $ad_id))->save($post);
            if($id)
            {
                $this->success("修改成功", U('Teach/adlist'));
            }
            else 
            {
                $this->error("修改失败");
            }
        }
        else 
        {
            $ad_id = I('get.id');
            $info = M('Teachad')->where(array('ad_id' => $ad_id))->find();
            $this->assign("info", $info);
            $this->display();
        }
    }
    
    public function delad()
    {
        $ad_id = I("get.id", "", "intval");
        if (!$ad_id) {
            $this->error("请指定需要删除的栏目！");
        }
        $status = M('Teachad')->where(array('ad_id' => $ad_id))->delete();
        if (false == $status) {
            $this->error('栏目删除失败！');
        }
        $this->success("栏目删除成功！", U("Teach/adlist"));
        
    }
    
    public function sort()
    {
        foreach ($_POST['sort'] as $id => $sort) {
            M('Teachad')->where(array('ad_id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }
    
    public function adbatch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
            }
            foreach ($ids as $id) {
                M('Teachad')->where(array('ad_id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }
    
    public function delAllad(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if(D('Admins/Teach')->deleteBatch($id)){
                    continue;
                }else{
                    $error = D('Admins/Teach')->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }
    
    public function topiclist()
    {
        if (IS_POST) {
            $this->redirect('topiclist', $_POST);
        }
        $get = I('get.');
        if (!empty($get['search'])) {
            $this->assign("search", $get['search']);
            //添加开始时间
            $start_time = I('get.start_time');
            if (!empty($start_time)) {
                $start_time = strtotime($start_time);
                $where['create_time'] = array("EGT", $start_time);
                $this->assign('start_time', $start_time);
            }
            //添加结束时间
            $end_time = I('get.end_time');
            if (!empty($end_time)) {
                $this->assign('end_times', $end_time);
                $end_time = strtotime($end_time);
                $where['create_time'] = array("ELT", $end_time);
                $this->assign('end_time', $end_time);
            }
            //时间搜索条件
            if ($end_time > 0 && $start_time > 0) {
                $where['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time));
            }
            
            $iflight = I('get.iflight', null, 'intval');
            if(!empty($iflight)){
                $this->assign("iflight", $iflight);
                $where['iflight'] = $iflight;
                if($iflight == 2)
                {
                    $where['iflight'] = 0;
                }
            }
            
            //搜索字段
            $type = I('get.type', null, 'intval');
            if(!empty($type)){
                $this->assign("type", $type);
                $where['type'] = $type;
            }
            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("keyword", $keyword);
                $where['title'] = array("LIKE", "%{$keyword}%");
            }
        }
        else 
        {
            $where['status'] = 1;
        }
        $count = M('Teachlist')->where($where)->count();
        $page = $this->page($count, 20);
        $list = M('Teachlist')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('create_time desc')->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                $list[$k]['title'] = hy_substr($v['title'], 20, true);
            }
        }
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        session('backurl',get_url());
        $this->display();
    }
    
    public function del_list()
    {
        $get = I('get.');
        $teachid = $get['teachid'];
        if(empty($teachid))
        {
            $this->error("没有指定删除的栏目");
        }
        $userid = M('Teachlist')->where(array('teachid' => $teachid))->getField('userid');
        $check = M('Teachlist')->where(array('teachid' => $teachid))->delete();
        if($check)
        {
            $data['upoint'] = array('exp', 'upoint-20');
            M('User')->where(array('userid' => $userid))->save($data);
            $this->success("成功删除");
        }
        else 
        {
            $this->error("删除失败");
        }
    }
    
    public function deleteAll_list()
    {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                $user = M('User')->where(array('teachid' => $id))->getField('userid');
                $check = M('Teachlist')->where(array('teachid' => $id))->delete();
                if($check){
                    $data['upoint'] = array('exp', 'upoint-20');
                    M('User')->where(array('userid' => $user))->save($data);
                    continue;
                }else{
                    $this->error('删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }
    
    public function setlight() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checklight':
                 //   $data=array('iflight'=>true, 'modify_time' => time());
                    $data['iflight'] = true;
                    $data['modify_time'] = time();
                    $msg='加精';
                    break;
                case 'cancellight':
                    $data=array('iflight'=>false);
                    $msg='取消加精';
                    break;
            }
            foreach ($ids as $id) {
                if($doAct == 'checklight')
                {
                    $info = M('Teachlist')->where(array('teachid' => $id))->field('userid, modify_time')->find();
                    if(!$info['modify_time'])
                    {
                        $tempdata['upoint'] = array('exp', 'upoint+30');
                        M('User')->where(array('userid' => $info['userid']))->save($tempdata);
                    }
                }
                M('Teachlist')->where(array('teachid'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }
}